<?php
/**
 * @title 1124. 表现良好的最长时间段 -
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $hours
     * @return Integer
     */
    function longestWPI($hours) {
        /**
         * @since 2020-07-31 队列记录
         */
        $len=count($hours);
        $list = array_fill(0, $len, -1);
        $list2 = $list;

        /**
         * @since 2020-07-31 栈和逆向栈
         */
        $stacks = [];
        $stacks2 = [];

        /**
         * @since 2020-07-28 遍历数组
         */
        $lastK2 = 0;
        for($i=0; $i<$len; $i++){
            if($hours[$i]>8){
                /**
                 * @since 2020-07-31 入栈
                 */
                $stacks[] = $i;

                /**
                 * @since 2020-07-31 逆向出栈
                 */
                $k2 = array_pop($stacks2);
                if($k2 !== null){
                    $list2[$k2] = $i;
                    $lastK2 = $k2;
                } else {
                    $list2[$lastK2] = $i;
                }
            } else {
                /**
                 * @since 2020-07-31 出栈
                 */
                $k = array_pop($stacks);
                if($k !== null){
                    $list[$k] = $i;
                } else {}

                /**
                 * @since 2020-07-31 逆向入栈
                 */
                $stacks2[] = $i;
            }
        }

        /**
         * @since 2020-07-31 收官, 如果有未弹出栈的数组, 直接到数组尾部
         */
        $right=$len-1;
        for($i=0, $stackLen=count($stacks); $i<$stackLen; $i++){
            $list[$stacks[$i]] = $right;
        }
        return [$list, $list2];

        /**
         * @since 2020-07-31 查找最长
         */
        for($i=0; $i<=$right; $i++){
            /**
             * @since 2020-07-31
             */
            
        }

        /**
         * @return
         */
        return $max;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.28_4.php
 */
$datas = [
    // [9,9,6,0,6,6,9],
    // [8,5,10,8,6,7,10,6],
    // [12,8,7,7,9,10,8,7,9,7,8,11],
    // [9,9,6,6,9],
    [9,9,6,6,9,9,6],
    // [6,9,9,6],
    // [6,9,9,9,6],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->longestWPI($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
